package com.youbug.mall.service_ucs.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.common.constant.AuthServerConstant;
import com.youbug.common.to.OrderTo;
import com.youbug.common.to.UmsMemberTo;
import com.youbug.common.vo.QueryVo;
import com.youbug.common.vo.R;
import com.youbug.mall.service_ucs.feign.IUcsMemberFeignService;
import com.youbug.mall.service_ucs.feign.IUcsOrderFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/mall/ucs")
public class ServiceFrontController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IUcsOrderFeignService ucsOrderFeignService;

    @GetMapping("/getLoginInfo")
    @ResponseBody
    private R<UmsMemberTo> getLoginInfo() {
        HttpSession session = request.getSession();
        if(session != null){
            UmsMemberTo memberResponseVo = (UmsMemberTo) session.getAttribute(AuthServerConstant.LOGIN_USER);
            return  R.ok(memberResponseVo);
        }
        return null;
    }

    @PostMapping("/getOrderList")
    @ResponseBody
    private R<IPage<OrderTo>> getOrderList(@RequestBody QueryVo<OrderTo> queryVo) {
        HttpSession session = request.getSession();
        if(session != null) {
            UmsMemberTo memberTo = (UmsMemberTo) session.getAttribute(AuthServerConstant.LOGIN_USER);
            if (memberTo == null) {
                throw new RuntimeException("登录信息已经过期");
            }
            queryVo.getPayload().setId(memberTo.getId());
            IPage<OrderTo> page =  ucsOrderFeignService.getOrderList(queryVo);
            return R.ok(page);
        }else{
            throw new RuntimeException("登录信息已经过期");
        }
    }

    @GetMapping("/getOrderInfo/{orderSn}/{skuId}")
    @ResponseBody
    private R<OrderTo> getOrderInfo(@PathVariable("orderSn") String orderSn, @PathVariable(value="skuId", required=false) String skuId) {
        OrderTo order =  ucsOrderFeignService.getOrderInfo(orderSn, skuId);
        return R.ok(order);
    }
}
